package com.zhangyu.eat.bomain;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhangyu.eat.R;
import com.zhangyu.eat.base.BaseActivity;
import com.zhangyu.eat.leishi.RecorderNoSkinActivity;
import com.zhangyu.eat.main.view.MainActivity;
import com.zhangyu.eat.me.FragmentFactory;
import com.zhangyu.eat.me.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FirstActivity extends BaseActivity {


    @BindView(R.id.first_tab)
    TabLayout firstTab;
    @BindView(R.id.first_Bing)
    ImageView firstBing;
    @BindView(R.id.activity_first)
    LinearLayout activityFirst;
    @BindView(R.id.f_Pager)
    CustomViewPager fPager;
    private List<Fragment> fragments;

    @Override
    protected void initView() {
        fragments = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Fragment fragment = FragmentFactory.creatFragment(i);
            fragments.add(fragment);
        }
        Vpadapter adapter = new Vpadapter(getSupportFragmentManager(), fragments);
        fPager.setAdapter(adapter);
        firstTab.setupWithViewPager(fPager);
        fPager.setPagingEnabled(false);
        for (int i = 0; i < firstTab.getTabCount(); i++) {
            TabLayout.Tab tab = firstTab.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTabView(i));
            }
        }

        firstBing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(FirstActivity.this, RecorderNoSkinActivity.class));
            }
        });
        firstTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                fPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_first;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public class Vpadapter extends FragmentStatePagerAdapter {
        private List<Fragment> fragments = new ArrayList<>();
        private int[] images = {R.drawable.main_selector, R.drawable.attention_selector, R.drawable.tab_logo_bingbing, R.drawable.message_selector, R.drawable.my_selector};
        private String[] title = {"广场", "关注", "", "私信", "我的"};

        public Vpadapter(FragmentManager fm, List<Fragment> mFragments) {
            super(fm);
            this.fragments = mFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        public View getTabView(int position) {
            //首先为子tab布置一个布局
            View v = LayoutInflater.from(FirstActivity.this).inflate(R.layout.tab, null);
            ImageView iv = (ImageView) v.findViewById(R.id.tab_image);
            iv.setImageResource(images[position]);
            TextView text = (TextView) v.findViewById(R.id.tab_text);
            text.setText(title[position]);
            return v;
        }
    }
}
